#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//教材存量表
//void purch()

struct booklist{
	char Class[20];   //班级 
	int ID;           //学号 
	char name[20];    //姓名 
	char ISBN[20];    //书号 
	char press[30];   //出版社 
	double price;     //单价 
	char add[50];     //地址 	
	char title[30];   //书名 
	int number;       //数量 
	char phone[30];   //电话 
}; 

int main()
{
	int i = 0;
	booklist book[200];
	FILE *fp;
	fp = fopen("pruchbook.txt","r");
	if(fp == NULL)
	{
		printf("未成功打开文件");
		return 0; 
	}
	while(!feof(fp))
	{
		fscanf(fp,"%s",&book[i].ISBN);
		fscanf(fp,"%d\n",&book[i].number);
		i++;
	}
	int n = i;
	printf("\t*********************************************************\t\n");
	printf("\t*\t\t\t待购教材表\t\t\t*\t\n");
	printf("\t*********************************************************\t\n");
	printf("\t*\t书号\t\t\t\t数量\t\t*\t\n");
	printf("\t*********************************************************\t\n");
	for(i=0;i<n;i++)
	{
		printf("\t*********************************************************\t\n");
		printf("\t*\t%s",book[i].ISBN);
		printf("\t\t%d\t\t*\t\n",book[i].number);
		printf("\t*********************************************************\t\n");
	}
	system("pause");
	system("cls");
	printf("\n\t\t是否要继续操作？\t\t");
	printf("\n\t\t1.登记\t\t");
	printf("\n\t\t2.修改\t\t");
	printf("\n\t\t3.退出\t\t"); 
	printf("\n\t请选择\t");
	int x;
	scanf("%d",&x);
	if(x == 1)
	{
		system("cls");
		printf("\n\t书号：");
		scanf("%s",&book[i].ISBN);
		getchar(); 
		printf("\n\t数量：");
		scanf("%d",&book[i].number);
		fp = fopen("pruchbook.txt","a");
	    if(fp == NULL)
	    {
		    printf("未成功打开文件");
			return 0; 
	    }
	    fprintf(fp,"\n%s %d",book[i].ISBN,book[i].number);
	    fclose(fp);
	    printf("\n\t信息录入成功!");
	}
	if(x == 2)
	{
		system("cls");
		char isbn[20];
		int number , flag = 0;
		printf("\n\t书号：");
		scanf("%s",isbn);
		getchar(); 
		printf("\n\t新数量：");
		scanf("%d",&number);
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn , book[i].ISBN) == 0)
			{
				flag = 1;
				book[i].number = number;
			}
			if(flag == 1)
			{
				break;
			}
		}
		if(flag == 0)
		{
			printf("\n\t未找到对应书号\n");
		}
		if(flag == 1)
		{
			fp = fopen("pruchbook.txt","w");
	        if(fp == NULL)
	        {
		        printf("未成功打开文件");
			    return 0; 
	        }
	        for(i=0;i<n;i++)
	        {
	        	fprintf(fp,"\n%s %d",book[i].ISBN,book[i].number);
			}
	        fclose(fp);
	        printf("\n\t信息修改成功!");
		}
	}
	if(x == 3)
	{
		return 0;
	}
	return 0;
}